using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using Possan.Graphics;

namespace Possan.Graphics.Filters
{


	// [Alias("Trim")]
	public class Trimmer : IBaseFilter
	{
		public bool Left;
		public bool Top;
		public bool Right;
		public bool Bottom;

		public Trimmer()
		{
			Left = true;
			Top = true;
			Right = true;
			Bottom = true;
		}

		public string ParameterDefinition
		{
			get
			{
				     return null;
			}
		}

		public Bitmap Filter(Bitmap bmp)
		{
			int w = bmp.Width;
			int h = bmp.Height;

			// plocka toplepft
			Color black = bmp.GetPixel(0, 0); // Color.FromArgb(0, 0, 0);

			int lefttrim = 0;
			if (Left)
			{
				while (Utilities.IsBackgroundOnly(bmp, lefttrim, 0, 1, h, black) && lefttrim < w)
					lefttrim++;
			}

			int righttrim = 0;
			if (Right)
			{
				while (Utilities.IsBackgroundOnly(bmp, (w - 1) - righttrim, 0, 1, h, black) && righttrim < w)
					righttrim++;
			}

			int toptrim = 0;
			if (Top)
			{
				while (Utilities.IsBackgroundOnly(bmp, 0, toptrim, w, 1, black) && toptrim < w)
					toptrim++;
			}

			int bottomtrim = 0;
			if (Bottom)
			{
				while (Utilities.IsBackgroundOnly(bmp, 0, (h - 1) - bottomtrim, w, 1, black) && bottomtrim < w)
					bottomtrim++;
			}

			int nw = bmp.Width - righttrim - lefttrim;
			int nh = bmp.Height - bottomtrim - toptrim;

			Bitmap neu = new Bitmap(nw, nh);
			neu.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);
			System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(neu);
			g.DrawImage(bmp, -lefttrim, -toptrim);

			     return neu;
		}
				  
	}
}





